﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Layout;
using System.Windows.Forms;
using System.Drawing;

namespace PasswordLocker.Controls
{
    /// <summary>
    /// Lays out child controls at the center of the parent control
    /// </summary>
    public class CenterLayoutEngine : LayoutEngine
    {
        public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
        {
            var parent = container as Control;
            if(parent == null)
            {
                return false;
            }

            // Use DisplayRectangle so that parent.Padding is honored.
            Rectangle parentDisplayRectangle = parent.DisplayRectangle;

            // find the center
            var center = new Point()
                   {
                       X = parentDisplayRectangle.Location.X + parentDisplayRectangle.Width/2,
                       Y = parentDisplayRectangle.Location.Y + parentDisplayRectangle.Height / 2,
                   };

            // place all controls in the center
            // Pretty rudimentary but thats all I need right now
            foreach(Control c in parent.Controls)
            {
                // Only apply layout to visible controls.
                if (!c.Visible)
                {
                    continue;
                }

                Size childSize = c.AutoSize ?
                                    c.GetPreferredSize(parentDisplayRectangle.Size) :
                                    c.Size;
                if (c.AutoSize)
                {
                    c.Size = childSize;
                }

                var controlLocation = new Point
                                          {
                                              X = center.X -
                                                  (childSize.Width +
                                                    c.Margin.Left + c.Margin.Right)/2,
                                              Y = center.Y -
                                                  (childSize.Height +
                                                    c.Margin.Top + c.Margin.Bottom) /2
                                          };

                // controlLocation shouldn't be past the parentDeisplayRectangle
                controlLocation.X = controlLocation.X < parentDisplayRectangle.Location.X
                                        ? parentDisplayRectangle.Location.X
                                        : controlLocation.X;
                controlLocation.Y = controlLocation.Y < parentDisplayRectangle.Location.Y
                                        ? parentDisplayRectangle.Location.Y
                                        : controlLocation.Y;
                // Set the location of the control.
                c.Location = controlLocation;
            }
            return parent.AutoSize;
        }
    }
}
